package com.xs.bookspoststation.StudentActivity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.xs.bookspoststation.BookActivity.BookViewActivity;
import com.xs.bookspoststation.BookActivity.UpdateBookActivity;
import com.xs.bookspoststation.R;
import com.xs.bookspoststation.adapter.BookAdapter;
import com.xs.bookspoststation.adapter.StudentAdapter;
import com.xs.bookspoststation.bean.Book;
import com.xs.bookspoststation.bean.Student;
import com.xs.bookspoststation.db.BookBean;
import com.xs.bookspoststation.db.DBHelperBook;
import com.xs.bookspoststation.db.DBHelperStudent;
import com.xs.bookspoststation.db.StudentBean;

import java.util.List;

public class StudentViewActivity extends AppCompatActivity {
    //定义对象
    TextView sel_sno;
    private ListView stu_listView;
    private Button stu_btn_back3;
    private List<Student> studentList;
    private StudentBean studentBean;
    //定义一个数据库帮助类
    private DBHelperStudent dbHelperStudent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_student_view);
        //绑定控件
        sel_sno = (TextView) findViewById(R.id.sel_sno);

        stu_listView = (ListView)findViewById(R.id.stu_listView);
        stu_btn_back3 = (Button)findViewById(R.id.stu_btn_back3);
        //返回按钮
        stu_btn_back3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(StudentViewActivity.this, OperateStudentActivity.class);
                Toast.makeText(getApplicationContext(), "返回上一页",Toast.LENGTH_LONG).show();
                startActivity(intent);
            }
        });
    }
    /**
     * 启动
     */
    @Override
    protected void onStart() {
        super.onStart();
        getData();
        StudentAdapter myAdapter=new StudentAdapter(this,R.layout.studentlist,studentList);
        stu_listView.setAdapter(myAdapter);
        stu_listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> arg0,View arg1,int arg2,long arg3) {
                final Student student = (Student) arg0.getItemAtPosition(arg2);
                final String sno = student.getSno();
                final String sname = student.getSname();
                final String spwd = student.getSpwd();
                final String sex = student.getSex();
                final String sphone = student.getSphone();
                final String sclass = student.getSclass();
                final String spro = student.getSpro();
                final String sde = student.getSde();
                //弹出框选择删除还是修改
                AlertDialog.Builder builder=new AlertDialog.Builder(StudentViewActivity.this);
                builder.setTitle("确定以下操作？？？");
                /**
                 * 修改逻辑代码
                 */
                builder.setPositiveButton("修改", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        //启动Activity
                        Intent intent=new Intent(StudentViewActivity.this, UpdateStudentActivity.class);
                        Bundle bundel=new Bundle();//传入页面数据
                        bundel.putString("sno", sno);
                        bundel.putString("sname", sname);
                        bundel.putString("spwd",spwd);
                        bundel.putString("sphone",sphone);
                        bundel.putString("sclass",sclass);
                        bundel.putString("spro",spro);
                        bundel.putString("sde",sde);
                        intent.putExtras(bundel);
                        startActivity(intent);
                    }});

                /**
                 * 删除逻辑代码
                 */
                builder.setNegativeButton("删除", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        //创建user对象
                        Student student = new Student(sno,sname,sex,spwd,sphone,sclass,spro,sde);
                        //打开数据库
                        SQLiteDatabase db = dbHelperStudent.getWritableDatabase();
                        if(db.isOpen()){
                            //直接根据学号sno判断学号是否存在
                            String sno = student.getSno();
                            //调用delete
                            db.delete("studentInfo","sno=?",new String[]{sno});
                            //打印日志
                            Log.v("删除学生信息操作","删除学生信息成功！");
                            Toast.makeText(getApplicationContext(), "删除成功", Toast.LENGTH_LONG).show();
                        }else{
                            Toast.makeText(StudentViewActivity.this,"数据库打开失败",Toast.LENGTH_LONG).show();
                        }
                        onStart();
                    }});
                builder.show();
            }});
    }

    /**
     * 调用查询方法
     */
    private void getData() {
        dbHelperStudent = new DBHelperStudent(StudentViewActivity.this,"student.db",null,1);
        studentBean = new StudentBean(this);
        studentList = studentBean.findAll();
    }
}
